/**
 * Created by Administrator on 2017/5/21 0021.
 */
layui.use('laydate', function() {
    var laydate = layui.laydate;

    var start = {
        min: laydate.now()
        , max: '2099-06-16 23:59:59'
        , istoday: false
        , choose: function (datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };

    var end = {
        min: laydate.now()
        , max: '2099-06-16 23:59:59'
        , istoday: false
        , choose: function (datas) {
            start.max = datas; //结束日选好后，重置开始日的最大日期
        }
    };

    document.getElementById('LAY_demorange_s').onclick = function(){
        start.elem = this;
        laydate(start);
    }
    document.getElementById('LAY_demorange_e').onclick = function(){
        end.elem = this
        laydate(end);
    }
});


layui.use(['laypage', 'layer'], function () {
    var laypage = layui.laypage, layer = layui.layer;
    laypage({
        cont: 'fullpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function (obj, first) {
            //得到了当前页，用于向服务端请求对应数据
            if (first != true) {//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var jsonObject = $('#jsonObject').val();
                console.log(jsonObject);
                if (jsonObject.length > 0) {
                    console.log(jsonObject);
                    var objs = eval('(' + jsonObject + ')');//将json转成对象
                    var condition = 'pageIndex=' + pageIndex + '&';
                    condition += "couponsVo.type=" + objs.type + '&' + "couponsVo.description=" + objs.description +
                        '&' + "couponsVo.startdate=" + objs.startdate + '&' + "couponsVo.enddate=" + objs.enddate;
                    console.log(condition);
                    window.location.href = "/front/mycoupons/querylist?" + condition;
                } else {
                    window.location.href = "/front/mycoupons/showdetail?pageIndex=" + pageIndex;
                }
            }
        }
    });
});

layui.use(['laypage', 'layer'], function () {
    var laypage = layui.laypage, layer = layui.layer;
    laypage({
        cont: 'mycoupon',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function (obj, first) {
            //得到了当前页，用于向服务端请求对应数据
            if (first != true) {//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var jsonObject = $('#jsonObject').val();
                console.log(jsonObject);
                window.location.href = "/front/mycoupons/showdetailbyMycoupons?pageIndex=" + pageIndex;

            }
        }
    });
});


layui.use('form', function () {
    var $ = layui.jquery, form = layui.form();


});

layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件

    var active = {
        receive: function (othis) {
            var params = $.param({
                cpid: othis.couponsid,

            }, true);

                $.ajax({
                    url: '/receive.action',
                    type: 'post',
                    data: params,
                    dataType: 'json',
                    success: function (data) {
                        console.log(data);
                        if (data == "领取成功") {
                            window.location.href = "/front/mycoupons/showdetail";//重新加载该页面
                            layer.msg("领取成功");
                        } else if(data == "优惠券不足"){
                            window.location.href = "/front/mycoupons/showdetail";
                            layer.msg("优惠券不足");
                        }else {
                            window.location.href = "/front/mycoupons/showdetail";
                            layer.msg("已经领取过了");
                        }
                    }

                })


        },
        delete: function (othis) {
            var con;
            con = confirm("删除后将不可恢复，是否确认删除?"); //在页面上弹出对话框
            if (con == true) {
                $.ajax({
                    url: "/delete.action",
                    type: 'post',
                    data: {
                        mycpid: othis.couponsid,
                    },
                    dataType: 'json',
                    success: function (data) {
                        if (data == "删除成功") {
                            window.location.href = "/front/mycoupons/showdetailbyMycoupons";
                            layer.msg("删除成功");
                        } else {
                            layer.msg("删除失败");
                        }
                    }
                });

            }
        },


    };
    $('.receive').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.couponsid = $(this).attr('couponsid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.delete').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.couponsid = $(this).attr('couponsid');
        active[method] ? active[method].call(this, othis) : '';
    });

});